package com.fingard.dsp.bank.directbank.cgb02;

import com.fingard.dsp.bank.directbank.bankInterface.IDeContract;
import com.fingard.dsp.bank.reqrespobj.contract.ContractState;
import com.fingard.dsp.bank.reqrespobj.decontract.ReqDeContract;
import com.fingard.dsp.bank.reqrespobj.decontract.RetAtsDeContract;
import com.fingard.dsp.bank.reqrespobj.decontract.detail.RetAtsDeContractDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class CGB02DeContract extends CGB02Base implements IDeContract {
    @Override
    public RetAtsDeContract deContract(ReqDeContract p_req) throws Exception {
        RetAtsDeContract retAts = new RetAtsDeContract(p_req.reqHeader);
        RetAtsDeContractDetail tmpRetDetail = new RetAtsDeContractDetail();
        tmpRetDetail.reqSeqID = p_req.contractDetail.askNumber;
        try {
            CGB02Header bankHeader = new CGB02Header();
            bankHeader.reqSeqID = p_req.contractDetail.askNumber;
            bankHeader.transCode = "2035";

            XmlTextWriter xmlWriter = new XmlTextWriter();
            writeCommonHeader(xmlWriter, bankHeader);
            xmlWriter.writeStartElement("Body");
            xmlWriter.writeElementString("signNo", p_req.contractDetail.askNumber);
            xmlWriter.writeElementString("signBankNo", p_req.contractDetail.authCode);
            xmlWriter.writeElementString("reserve1", "");
            xmlWriter.writeElementString("reserve2", "");
            xmlWriter.writeElementString("reserve3", "");
            xmlWriter.writeEndDocument();

            String[] tmpRetStr = sendToBank(xmlWriter.toXmlString());

            if(tmpRetStr[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpRetStr[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    tmpRetDetail.respCode = "0";

                    String tmpRespCode = tmpBkReader.getFirstText("/BEDC/Message/commHead/retCode");
                    if ("000".equals(tmpRespCode)) {
                        String transStatus = tmpBkReader.getFirstText("/BEDC/Message/Body/tranStatus");
                        if ("0".equals(transStatus)) {
                            tmpRetDetail.deSignState = ContractState.SUCCESS;
                        } else {
                            tmpRetDetail.deSignState = ContractState.FAIL;
                        }
                        String transInfo = tmpBkReader.getFirstText("/BEDC/Message/Body/errMsg");;
                        if (StringHelper.hasAnyChar(transInfo)) {
                            tmpRetDetail.respInfo = transInfo;
                        }
                    } else {
                        tmpRetDetail.respCode = "-1";
                        tmpRetDetail.respInfo = tmpRespCode;
                    }
                } else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = tmpRetStr[1];
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }

    @Override
    public RetAtsDeContract qryDeContract(ReqDeContract p_reqQry) throws Exception {
        return null;
    }
}
